﻿var loginFormCanSubmit = false;
$(function(){
	init('#loginForm');
	readRememberPass('#rememberPass input');
	$('#loginForm').submit(function(){ formSbmit(this); return false; });
});

function init(obj){
	$('#rememberPass').slideDown('slow');
};

/*function checkForm(obj){
	if( $('#userName').val() == '' ){
		$.openWin({'msg':'请填写用户名','status':'error','time':2000});
		return false;
	}
	
	if( !isEmail($('#userName').val())){
		$.openWin({'msg':'用户名格式错误','status':'error','time':2000});
		return false;
	}
	
	if( $('#password').val() == '' ){
		$.openWin({'msg':'请填写密码','status':'error','time':2000});
		return false;
	}
	return true;	
}*/


function formSbmit(obj){
	var $obj = $(obj);
	//if(checkForm(obj)){
		$obj.find('.submit').attr('disabled',true);
		$.openWin({'msg':loadingBar,'canClose':false });
		var url = $obj.attr('action')+'&time='+time();
		var userName = $('#userName').val();
		var password = $('#password').val();
		var referer = escape($('#referer').val()) ;
		var sendData = '&userName='+userName +'&password='+ password+'&referer='+ referer +'&time='+time() ;//+'&jsoncallback=?';
		$.post(url , sendData,function(data){
			if('ok'== data.status){
				rememberPass('#rememberPass input');
				$.openWin({'msg': data.msg,'canClose':false});
				setTimeout(function(){
					if( loginType == 'mini'){
						window.parent.loginMiniSuccess();
					}else
						window.location.href= data.url;
				},2000);				
			}else{
				$.openWin({'msg': data.msg,'status':data.status});
			}
			$obj.find('.submit').attr('disabled',false);
		},'json');
	//}else{
//		$.openWin({msg:'请正确填写<span class="high">所有项目</span>','time':200000,'status':'error'});
//	}
};


function rememberPass(obj){
	var $obj = $(obj);
	if($obj.attr('checked')){
		$.cookie('isSaveLoginInfo','1',{'expires':1000});
		$.cookie('r_username',$('#userName').val(),{'expires':1000});
		$.cookie('r_password',$('#password').val(),{'expires':1000});
	}else{
		$.cookie('r_username','',{'expires':0.0000001});
		$.cookie('r_password','',{'expires':0.0000001});
		$.cookie('isSaveLoginInfo','',{'expires':0.0000001});
	};
};

function readRememberPass(obj){
	if( '1' == $.cookie('isSaveLoginInfo') ){
		$('#userName').val($.cookie('r_username'));
		$('#password').val($.cookie('r_password'));
		$(obj).attr('checked',true);
	};
};